package com.kkb.kkbadmin.controller;


import com.github.pagehelper.PageInfo;
import com.kkb.kkbadmin.domain.ProjectInfo;
import com.kkb.kkbadmin.domain.User;
import com.kkb.kkbadmin.service.BrowsingHistoryService;
import com.kkb.kkbadmin.service.ProjectManagementService;
import com.kkb.kkbadmin.service.ProjectRegisterService;
import com.kkb.kkbcommon.api.CommonPage;
import com.kkb.kkbcommon.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @ClassName ProjectManagementController
 * @Description 项目管理后台controller
 * @Author 侯兴飞
 * @Date 2021/8/12
 **/
@RestController
@RequestMapping("project")
@Api(tags = "项目管理API", value = "项目上传、项目列表分页查询、项目详情查看、删除和修改项目、修改项目状态、导师审核")
public class ProjectManagementController {

    @Resource
    private ProjectManagementService pmService;
    @Resource
    private BrowsingHistoryService historyService;
    @Resource
    private ProjectRegisterService projectRegisterService;

    /**
     * 分页查询项目列表
     *
     * @param condition 条件查询的条件
     * @param pageNum   当前页码
     * @param pageSize  每页条数
     * @param user      用户信息
     * @return CommonResult<PageInfo>
     */
    @ApiOperation(value = "分页查询", notes = "分页查询项目列表")
    @RequestMapping(value = "/projects", method = RequestMethod.GET)
    public CommonResult<CommonPage<ProjectInfo>> findPage(@Validated @RequestBody ProjectInfo condition,
                                                          @Validated @RequestBody User user,
                                                          @Validated @RequestParam(defaultValue = "1") int pageNum,
                                                          @Validated @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<ProjectInfo> page = pmService.findPage(condition, user, pageNum, pageSize);
        //分页对象封装到 CommonPage<ProjectInfo>
        CommonPage<ProjectInfo> commonPage = CommonPage.restPage(page.getList());
        //返回封装结果
        return CommonResult.success(commonPage);
    }

    /**
     * 根据id查询项目详情
     *
     * @param id
     * @return CommonResult<ProjectInfo>
     */
    @ApiOperation(value = "查询项目详情", notes = "根据id查询项目详情")
    @GetMapping("/project/{id}")
    public CommonResult<ProjectInfo> findById(@Validated @PathVariable("id") Long id) {
        //查询项目对象
        ProjectInfo projectInfo = pmService.findById(id);
        //todo:插入一条浏览记录
        return CommonResult.success(projectInfo);
    }

    /**
     * 项目上传
     * 用@requestBody接收多个数据
     *
     * @param info 项目信息
     * @return CommonResult<ProjectInfo>
     */
    @ApiOperation(value = "新建项目", notes = "后台管理员上传项目。")
    @RequestMapping(value = "/project", method = RequestMethod.POST)
    public CommonResult<ProjectInfo> doAdd(@Validated @RequestBody ProjectInfo info) {
        pmService.add(info);
        return CommonResult.success(null);
    }

    /**
     * 修改项目状态 0报名中、1进行中、2已完成、3已取消
     *
     * @param id 项目id
     * @return 结果集
     */
    @ApiOperation(value = "修改项目状态", notes = "导师学生审核通过后修改项目的状态")
    @PutMapping("/project/{id}")
    public CommonResult updateStatus(@Validated @PathVariable("id") Long id, @Validated @RequestParam Integer status) {
        pmService.updateStatus(id, status);
        return CommonResult.success(null);
    }

    /**
     * 根据项目id修改项目
     *
     * @param projectInfo 项目对象
     * @param id          项目id
     * @return CommonResult<ProjectInfo>
     */
    @ApiOperation(value = "更新项目信息", notes = "后台管理员修改项目信息")
    @RequestMapping(value = "/project/{id}", method = RequestMethod.PUT)
    public CommonResult<ProjectInfo> update(@Validated @RequestBody ProjectInfo projectInfo, @Validated @PathVariable("id") long id) {
        pmService.update(projectInfo, id);
        return CommonResult.success(null);
    }

    /**
     * 根据项目id删除项目
     *
     * @param id 项目id
     * @return CommonResult<ProjectInfo>
     * @Author 甘
     */
    @ApiOperation(value = "删除项目信息", notes = "后台管理员删除项目")
    @DeleteMapping("/project/{id}")
    public CommonResult<ProjectInfo> delete(@Validated @PathVariable("id") long id) {
        pmService.delete(id);
        return CommonResult.success(null);
    }

    /**
     * 导师审核 ：修改导师报名表的状态为已报名，修改项目表的项目状态为进行中
     *
     * @param user      项目导师
     * @param projectId 项目id
     * @return CommonResult
     */
    @ApiOperation(value = "审核导师", notes = "管理员审核导师，修改导师报名状态，修改项目状态")
    @PutMapping("/teacher/{id}")
    public CommonResult auditTeacher(@RequestBody @Validated User user, @Validated @PathVariable("id") Long projectId) {
        pmService.auditTeacher(user, projectId);
        return CommonResult.success(null);
    }
}

